
package com.probiz.estore.sales.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.probiz.estore.common.model.catalog.ProductCategory;
import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.sales.RecommendedProduct;
import com.probiz.estore.common.model.sales.RecommendedType;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.sales.SalesConstants;
import com.probiz.estore.sales.dao.RecommendedProductDao;
import com.probiz.estore.sales.dao.RecommendedTypeDao;
import com.probiz.estore.sales.service.RecommendedProductManager;

public class RecommendedProductManagerImpl extends
		GenericManagerImpl<RecommendedProduct> implements
		RecommendedProductManager {

	private RecommendedProductDao	recommendedProductDao	= null;
	private RecommendedTypeDao		recommendedTypeDao		= null;

	public void setRecommendedProductDao(
			RecommendedProductDao recommendedProductDao) {
		this.recommendedProductDao = recommendedProductDao;
	}

	public RecommendedTypeDao getRecommendedTypeDao() {
		return recommendedTypeDao;
	}

	public void setRecommendedTypeDao(RecommendedTypeDao recommendedTypeDao) {
		this.recommendedTypeDao = recommendedTypeDao;
	}

	protected void initManager() {
		dao = recommendedProductDao;
	}

	protected void preDelete(RecommendedProduct entity) {

	}

	protected void preSave(RecommendedProduct entity) {

	}
	
	

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#save(java.lang.Object)
	 */
	public void save(RecommendedProduct recommendedProduct) {
		RecommendedType recommendedType = recommendedTypeDao
				.getById(recommendedProduct.getRecommendedTypeId());
		if (!recommendedType.getRuleCode().equals(
				SalesConstants.RECOMMENDED_RULE_CODE_SIMILAR_PRODUCT)) {
			recommendedProductDao.save(recommendedProduct);
		} else {
			saveSimilarProducts(recommendedProduct);
		}
	}

	// 重载deleteById方法
	public void deleteById(Integer recommendedProductId) {
		RecommendedProduct recommendedProduct = loadById(recommendedProductId);
		delete(recommendedProduct);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#delete(java.lang.Object)
	 */
	public void delete(RecommendedProduct recommendedProduct) {
		RecommendedType recommendedType = recommendedTypeDao
				.getById(recommendedProduct.getRecommendedTypeId());
		if (!recommendedType.getRuleCode().equals(
				SalesConstants.RECOMMENDED_RULE_CODE_SIMILAR_PRODUCT)) {
			recommendedProductDao.delete(recommendedProduct);
		} else {
			removeSimilarProducts(recommendedProduct);
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.sales.service.RecommendedProductManager#saveSimilarProducts(com.probiz.estore.common.model.sales.RecommendedProduct)
	 */
	public void saveSimilarProducts(RecommendedProduct recommendedProduct) {
		recommendedProductDao.save(recommendedProduct);
		RecommendedProduct similarProduct = recommendedProductDao.getRecommendedProduct(recommendedProduct.getRecommendedTypeId(), recommendedProduct.getProductId(),null, recommendedProduct.getSourceProductId());
		if (similarProduct == null) {
			similarProduct = new RecommendedProduct();
		}
		similarProduct.setSourceProductId(recommendedProduct.getProductId());
		similarProduct.setProductId(recommendedProduct.getSourceProductId());
		similarProduct.setRecommendedTypeId(recommendedProduct.getRecommendedTypeId());
		similarProduct.setSortOrder(recommendedProduct.getSortOrder());
		recommendedProductDao.save(similarProduct);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.sales.service.RecommendedProductManager#removeSimilarProducts(com.probiz.estore.common.model.sales.RecommendedProduct)
	 */
	public void removeSimilarProducts(RecommendedProduct recommendedProduct) {
		if (recommendedProduct != null) {
			// 相似产品要同时删除两个相对应的产品，这时查找的sourceId与productId位置相换
			RecommendedProduct similarProduct = recommendedProductDao
					.getRecommendedProduct(recommendedProduct
							.getRecommendedTypeId(), recommendedProduct
							.getProductId(), null,recommendedProduct.getSourceProductId());

			if (similarProduct != null) {
				recommendedProductDao.delete(similarProduct);
			}
			recommendedProductDao.delete(recommendedProduct);
		}
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.sales.service.RecommendedProductManager#saveRecommendedProductsFromArrays(com.probiz.estore.common.model.product.Product, com.probiz.estore.common.model.catalog.ProductCategory, java.lang.String[], java.lang.String[])
	 */
	public void saveRecommendedProductsFromArrays(Product sourceProduct,ProductCategory sourceCategory,String[] recommendedTypeIds,String[] productIds){
		List<RecommendedProduct> formerRpList = new ArrayList<RecommendedProduct>(); //必须用这种方法 ArrayList支持remove方法
		int sourceKind;
		Integer sourceId;
		if(null != sourceProduct){
			//原来的推荐商品
			for(RecommendedProduct formerRp : sourceProduct.getRecommendedProducts()){
				formerRpList.add(formerRp);
			}
			sourceKind = SalesConstants.RECOMMENDED_SOURCE_KIND_PRODUCT;
			sourceId = sourceProduct.getProductId();
		}else{
			//原来的推荐商品
			for(RecommendedProduct formerRp : sourceCategory.getRecommendedProducts()){
				formerRpList.add(formerRp);
			}
			sourceKind = SalesConstants.RECOMMENDED_SOURCE_KIND_PRODUCTCATEGORY;
			sourceId = sourceCategory.getCategoryId();
		}	
		
		
		
		for(int i=0; i< recommendedTypeIds.length; i++){
			RecommendedProduct rp = recommendedProductDao.getRecommendedProduct(Integer.parseInt(recommendedTypeIds[i]), sourceKind, sourceId, Integer.parseInt(productIds[i]));
			if(null != rp){
				//修改
				rp.setSortOrder(i);
				if(formerRpList.contains(rp)){
					formerRpList.remove(rp);
				}
			}else{
				//新增
				rp = new RecommendedProduct();
				rp.setRecommendedTypeId(Integer.parseInt(recommendedTypeIds[i]));
				rp.setSourceProduct(sourceProduct);
				rp.setSourceCategory(sourceCategory);
				rp.setProductId(Integer.parseInt(productIds[i]));
				rp.setSortOrder(i);
				rp.setSourceKind(Short.valueOf((short)sourceKind));
			}
			recommendedProductDao.save(rp);
		}	
		
		//删除
		for(RecommendedProduct rp: formerRpList){
			recommendedProductDao.delete(rp);
		}
		
	}

	public List<Product> getProductsByRecommendedTypeIdBySourceId(
			Integer recommendedTypeId, int sourceKind, Integer sourceId,
			int firstResult, int maxResults) {
		return recommendedProductDao.getProductsByRecommendedTypeIdBySourceId(recommendedTypeId, sourceKind, sourceId, firstResult, maxResults);
	}

	public List<Product> getProductsByRecommendedTypeNameBySourceId(
			String recommendedTypeName, int sourceKind, Integer sourceId,
			int firstResult, int maxResults) {
		return recommendedProductDao.getProductsByRecommendedTypeNameBySourceId(recommendedTypeName, sourceKind, sourceId, firstResult, maxResults);
	}

	public List<Product> getProductsByRecommendedTypeNameBySourceIdList(
			String recommendedTypeName, int sourceKind,
			List<Integer> sourceIds, int firstResult, int maxResults) {
		return recommendedProductDao.getProductsByRecommendedTypeNameBySourceIdList(recommendedTypeName, sourceKind, sourceIds, firstResult, maxResults);
	}

	public RecommendedProduct getRecommendedProduct(Integer recommendedTypeId,
			int sourceKind, Integer sourceId, Integer productId) {
		return recommendedProductDao.getRecommendedProduct(recommendedTypeId, sourceKind, sourceId, productId);
	}

	public List<RecommendedProduct> getRecommendedProductsByRecommendedTypeIdBySourceId(
			int recommendedTypeId, Integer sourceKind, Integer sourceId,
			int firstResult, int maxResults) {
		return recommendedProductDao.getRecommendedProductsByRecommendedTypeIdBySourceId(recommendedTypeId, sourceKind, sourceId, firstResult, maxResults);
	}

	

}
